package com.yangjun.utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.google.common.collect.Maps;

public class EchartUtils {
	
    /**
     * 返回列值
     * @param yValue 列值
     * @param list 数据集合
     * @return
     */
    public static List<Object> getSignColumn(String yValue,List<Map<String, Object>> list){
    	List<Object> yList = new ArrayList<Object>();
    	for(int i=0;i<list.size();i++){
    		Map<String,Object> map = list.get(i);
    		yList.add(String.valueOf(map.get(yValue)));
    	}
    	return yList;
    }
    
    /**
     * 多条曲线去同一个数据集合获取Y数据时,获取相同维度的数据
     * @param xValue
     * @param yValue
     * @param list
     * @return
     */
    public static List<Object> getSignColumnByXvalue(Object xValue,String yValue,List<Map<String, Object>> list){
    	List<Object> yList = new ArrayList<Object>();
    	for(int i=0;i<list.size();i++){
    		Map<String,Object> map = list.get(i);
    		if(xValue.equals(map.get(xValue))){
    			yList.add(map.get(yValue));
    		}
    	}
    	return yList;
    }
    
    /**
     * int 类型
     * 返回y轴的数组值-->当x轴与y轴列不一样 需要补0的情况下
     * 单线条折线图  Y轴的值多于X轴的值
     * @param xAxis x轴列对应的数据列
     * @param yValue  y轴列对应的数据列
     * @param legend
     * @param list list 数据集合
     * @param xList x轴 数据集合
     * @return
     */
    public static int[]  getSignColumnOfYvalueIntSz(String xAxis,String yValue,List<Map<String, Object>> list ,List<String> xList){
    	
    	int[] columnSz = new int[xList.size()];
    	
    	 for(int i=0;i<list.size();i++){
    		 Map<String,Object> map = list.get(i);
             String xvalue = String.valueOf(map.get(xAxis));
             if(xList.indexOf(xvalue)>0){
            	 columnSz[xList.indexOf(xvalue)] = Integer.parseInt(map.get(yValue).toString());
             }
    	 }
         return columnSz;
    }
    /**
     * 
     * float 类型
     * 返回y轴的数组值-->当x轴与y轴列不一样 需要补0的情况下
     * 单线条折线图  Y轴的值多于X轴的值
     * @param xAxis x轴列对应的数据列
     * @param yValue  y轴列对应的数据列
     * @param legend
     * @param list list 数据集合
     * @param xList x轴 数据集合
     * @return
     */
    public static float[]  getSignColumnOfYvalueFloatSz(String xAxis,String yValue,List<Map<String, Object>> list ,List<String> xList){
    	
    	float[] columnSz = new float[xList.size()];
    	
    	for(int i=0;i<list.size();i++){
    		Map<String,Object> map = list.get(i);
    		String xvalue = String.valueOf(map.get(xAxis));
    		if(xList.indexOf(xvalue)>0){
    			columnSz[xList.indexOf(xvalue)] = Float.parseFloat(map.get(yValue).toString());
    		}
    	}
    	return columnSz;
    }
    
    /**
     * 折线图 -- > X轴与Y轴一一对应
     * @param xAxis x轴列对应的数据列
     * @param yValue  y轴列对应的数据列
     * @param legend  
     * @param list 数据集合
     * @return
     */
    public static Map<String,Object>  getBrokenOneLineMap(String xAxis,String yValue,String[] legend,List<Map<String, Object>> list){
    	
    	Map<String,Object> returnResult = Maps.newHashMap(); 
    	List<Object> xList = new ArrayList<Object>();
    	List<Object> yList = new ArrayList<Object>();
    	for(int i=0;i<list.size();i++){
   		 Map<String,Object> map = list.get(i);
   		 xList.add(String.valueOf(map.get(xAxis)));
   		 yList.add(String.valueOf(map.get(yValue)));
    	}
    	returnResult.put("legend", legend);
    	returnResult.put("xAxis", xList);
    	returnResult.put("yvalue", yList);
		return returnResult;
    	
    }
    
    public static Map<String,Object>  getBrokenOnePieMap(String keycolumn,String valuecolumn,List<Map<String, Object>> list){
		 Map<String,Object> returnMap = Maps.newHashMap();
		 List<Object> keyLegend=new ArrayList<Object>();
		 List<Map<String, Object>> dataList = new ArrayList<Map<String, Object>>();
		 for(Map<String, Object> val : list){
			 Map<String, Object> date = new HashMap<String, Object>();
			 
			 Object key = val.get(keycolumn);
			 Object value = val.get(valuecolumn);
			 date.put("name", key);
			 date.put("value", value);
			 keyLegend.add(key);
			 dataList.add(date);
			 
		 }
		 returnMap.put("legend", keyLegend);
		 returnMap.put("data", dataList);
		 return returnMap;
    }
    
    
    public static Map<String, Object> resultMap(String today,String yesterday, String contrastday,List<Map<String, Object>> dataList){
    	Map<String, Object> resultMap=new HashMap<String, Object>();
    	if(dataList==null){
    		 resultMap.put("contrastday",null);
 	   		 resultMap.put("yesterday",null);
 		   	 resultMap.put("today",null);
 		   	 return resultMap;
    	}
    	
    	
    	 if(dataList.size()==3){
    		 Map<String, Object> one = dataList.get(0);
    		 Map<String, Object> two = dataList.get(1);
    		 Map<String, Object> three = dataList.get(2);
 	   		if(one!=null){
 	   			if(contrastday.equals(one.get("data_date"))){
 	   				resultMap.put("contrastday",one);
 	   			}else if(yesterday.equals(one.get("data_date"))){
 	   				resultMap.put("yesterday",one);
 	   			}if(today.equals(one.get("data_date"))){
 	   				resultMap.put("today",one);
 	   			}
 	   		}
 	   		if(two!=null){
 	   			if(contrastday.equals(two.get("data_date"))){
 	   				resultMap.put("contrastday",two);
 	   			}else if(yesterday.equals(two.get("data_date"))){
 	   				resultMap.put("yesterday",two);
 	   			}if(today.equals(two.get("data_date"))){
 	   				resultMap.put("today",two);
 	   			}
 	   		}
 	   		if(three!=null){
 	   			if(contrastday.equals(three.get("data_date"))){
 	   				resultMap.put("contrastday",three);
 	   			}else if(yesterday.equals(three.get("data_date"))){
 	   				resultMap.put("yesterday",three);
 	   			}if(today.equals(three.get("data_date"))){
 	   				resultMap.put("today",three);
 	   			}
 	   		}
 	   	 }else if(dataList.size()==2){
     	   	 Map<String, Object> one = dataList.get(0);
    		 Map<String, Object> two = dataList.get(1);
 	   		if(one!=null){
 	   			if(contrastday.equals(one.get("data_date"))){
 	   				resultMap.put("contrastday",one);
 	   			} 
 	   				
 	   			if(yesterday.equals(one.get("data_date"))){
 	   				resultMap.put("yesterday",one);
 	   			}
 	   			
 	   			if(today.equals(one.get("data_date"))){
 	   				resultMap.put("today",one);
 	   			}
 	   		}
 	   		if(two!=null){
 	   			if(contrastday.equals(two.get("data_date"))){
 	   				resultMap.put("contrastday",two);
 	   			}
                if(yesterday.equals(two.get("data_date"))){
 	   				resultMap.put("yesterday",two);
 	   			}
                if(today.equals(two.get("data_date"))){
 	   				resultMap.put("today",two);
 	   			}
 	   		}
 	   	 }else if(dataList.size()==1){
     	   	 Map<String, Object> one = dataList.get(0);
 	   		if(one!=null){
 	   			if(contrastday.equals(one.get("data_date"))){
 	   				resultMap.put("contrastday",one);
 	   			}else if(yesterday.equals(one.get("data_date"))){
 	   				resultMap.put("yesterday",one);
 	   			}else if(today.equals(one.get("data_date"))){
 	   				resultMap.put("today",one);
 	   			}
 	   		}
 	   	 }else{
 	   		 resultMap.put("contrastday",null);
 	   		 resultMap.put("yesterday",null);
 		   	 resultMap.put("today",null);
 	   	 }
    	 
    	 return resultMap;
    }
  }
